//
//  ReadCollectionViewCell.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/23.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadCollectionViewCell.h"

@implementation ReadCollectionViewCell

- (void)dealloc {
    
    _readTableView.delegate = nil;
    _readTableView.dataSource = nil;
    
    [_readTableView release];
    [_essayArr release];
    [_serialArr release];
    [_questionArr release];
    [_dataSource release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.essayArr = [NSMutableArray array];
        self.serialArr = [NSMutableArray array];
        self.questionArr = [NSMutableArray array];
       
        self.readTableView = [[YHYBaseTableView alloc] initWithFrame:CGRectZero];
        [self.readTableView registerClass:[ReadTableViewCell class] forCellReuseIdentifier:@"readT"];
        self.readTableView.delegate = self;
        self.readTableView.dataSource = self;

        [self.contentView addSubview:_readTableView];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.readTableView.frame = self.contentView.bounds;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"readT"];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.layer.cornerRadius = 5;
//    cell.layer.masksToBounds = YES;
//    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    cell.layer.borderWidth = 2;
    
//    switch (indexPath.row) {
//        case 0:
//            cell.label.text = @"问题";
//            cell.essayModel = self.essayArr[indexPath.row];
//            break;
//        case 1:
//            cell.label.text = @"连载";
//            cell.serialModel = self.serialArr[indexPath.row];
//            
//        default:
//            cell.label.text = @"短篇";
//            cell.questionModel = self.questionArr[indexPath.row];
//            break;
//    }
    if (indexPath.row == 0) {
        cell.label.text = @"短篇";
        cell.essayModel = self.essayModel;
    }
    else if (indexPath.row == 1) {
        cell.label.text = @"连载";
        cell.serialModel = self.serialArr[indexPath.row];
    }
    else {
        cell.label.text = @"问题";
        cell.questionModel = self.questionModel;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
            
            EssayModel *model = self.essayModel;
            CGFloat height = [ReadTableViewCell heightWithText:model.guide_word];
            return height + 70;
        
    }
    else if (indexPath.row == 1) {
        
        
        if (_serialArr.count != 0) {
            SerialModel *model = self.serialArr[indexPath.row];
            CGFloat height = [ReadTableViewCell heightWithText:model.excerpt];
             return height + 70;
            
        }

    }
    
        QuestionModel *model = self.questionModel;
        CGFloat height = [ReadTableViewCell heightWithText:model.answer_content];
        return height + 70;
  
    return 0;
}

- (void)setEssayModel:(EssayModel *)essayModel {
    if (_essayModel != essayModel) {
        [_essayModel release];
        _essayModel = [essayModel retain];
    }
    [self.readTableView reloadData];
}

- (void)setSerialModel:(SerialModel *)serialModel {
    if (_serialModel != serialModel) {
        [_serialModel release];
        [_serialModel retain];
    }
   [self.readTableView reloadData];
}

- (void)setSerialArr:(NSMutableArray *)serialArr {
    if (_serialArr  != serialArr) {
        [_serialArr release];
        _serialArr = [serialArr retain];
    }
    
    [_readTableView reloadData];
}

-(void)setQuestionModel:(QuestionModel *)questionModel {
    if (_questionModel != questionModel) {
        [_questionModel release];
        _questionModel = [questionModel retain];
    }
    
    [self.readTableView reloadData];
   
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.readTableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpNext" object:@(indexPath.row)];
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
